package com.czumi.javawx.web.controller.admin;

import javax.servlet.http.HttpSession;

import org.jboss.logging.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.czumi.javawx.constants.AppConst;
import com.czumi.javawx.constants.UrlConst;
import com.czumi.javawx.dao.entity.SysConfig;
import com.czumi.javawx.dao.entity.SysUser;
import com.czumi.javawx.service.SysUserService;
import com.czumi.javawx.util.MD5;
import com.czumi.javawx.web.form.ResultDto;

/**
 * 
 * @author 袁友林
 * @date 2017年10月26日 下午8:17:28
 */
@Controller
public class LoginController {
	
	@Autowired
	private SysConfig sysConfig;
	
	@Autowired
	private SysUserService sysUserService;
	
	
	@RequestMapping(value=UrlConst.URL_ADMIN_LOGIN,method=RequestMethod.GET)
	public String login() {		
		System.out.println(sysConfig.getToken());
		return UrlConst.URL_ADMIN_LOGIN_WEB;
	}
	
	@RequestMapping(value=UrlConst.URL_ADMIN_LOGIN,method=RequestMethod.POST)	
	public  String  doLogin(SysUser sysUser,HttpSession session) {
		ResultDto resultDto=new ResultDto();
		sysUser.setPassword(MD5.md5(sysUser.getPassword()));
		SysUser tmp= sysUserService.getUserByCondition(sysUser);
		if(tmp!=null) {
			session.setAttribute(AppConst.SESSION_ADMIN_KEY, tmp);
			return "redirect:"+UrlConst.URL_ADMIN_INDEX;
		}else {
			return  UrlConst.URL_ADMIN_LOGIN_WEB;
		}
	}
	
	@RequestMapping(value=UrlConst.URL_ADMIN_LOGOUT,method=RequestMethod.GET)
	public String  doLogout(SysUser sysUser,HttpSession session) {
		session.removeAttribute(AppConst.SESSION_ADMIN_KEY);
		return "redirect:"+UrlConst.URL_ADMIN_LOGIN;
	}
	
	
}
  
